/*
 * @Date: 2023-07-11 14:06:11
 * @LastEditors: 景冰川 961166897@qq.com
 * @LastEditTime: 2023-08-25 16:07:38
 * @FilePath: \part-cloud-appd:\project\company\veteran-cadre-app\src\stores\app-store.ts
 */
import { defineStore } from 'pinia'
import type { IappInfo, IloginInfo } from '@/api/app/index.d'
import { appLogin, getInfo } from '@/api/app'
import type { RoleI } from '@/model/index.d'

export const useAppStore = defineStore('app', () => {
  interface StateI {
    token: string
    info: IappInfo
    role: RoleI
    code: string
    appId: string
    loginRes: Api.BasicInstance
    loginInfoRes: IloginInfo
    showSfyz: boolean
    bottomHeight: string
  }
  const state = reactive<StateI>({
    token: '',
    info: null,
    role: null,
    code: '',
    appId: '',
    loginRes: null,
    loginInfoRes: null,
    showSfyz: false,
    bottomHeight: '74px',
  })

  const setStateInfo = (info: IappInfo) => {
    state.info = info
  }

  const setStateRole = (role: RoleI) => {
    state.role = role
  }
  const setStateCode = (code: string) => {
    state.code = code
  }
  const setStateAppId = (appId) => {
    state.appId = appId
  }

  function setStateToken(token: string) {
    state.token = token
  }
  function clearToken() {
    state.token = ''
  }

  function setLoginRes(loginRes: Api.BasicInstance) {
    state.loginRes = loginRes
  }

  function setLoginInfoRes(loginInfoRes: IloginInfo) {
    state.loginInfoRes = loginInfoRes
  }

  const login: () => Promise<Api.BasicInstance> = () => {
    return new Promise((resolve, reject) => {
      uni.login({
        provider: 'weixin',
        async success(res) {
          if (res) {
            setStateCode(res.code)
            const param = {
              code: res.code,
              appKey: state.appId,
              // type: state.role?.type,
            }
            const loginRes = await appLogin(param)
            setLoginRes(loginRes)
            resolve(loginRes)
            if (loginRes.code === 200)
              setStateToken(loginRes.data as unknown as string)
          }
        },
      })
    })
  }
  // 1 （未审核） - 身份认证 2 （待审核，已驳回） - 请联系管理员审核 3 （通过）- 可以点击
  function onClickDis() {
    if (!state.loginRes) {
      uni.showToast({ title: '请先登录', icon: 'none' })
      return 0
    }

    if (state.loginRes.code === 10011) {
      uni.showModal({
        title: '提示',
        content: '还未注册？立即注册',
        success: (res) => {
          if (res.confirm)
            router.push(`/pages-sub/user/identity?backBtn=${true}&pageType=0`)
            // uni.reLaunch({
            //   url: `/pages-sub/user/identity?backBtn=${false}&pageType=0`,
            // })
        },
      })
      // state.showSfyz = true
      return 1
    }
    if (state.loginRes.code === 10012) {
      uni.showToast({ title: '请联系管理员审核', icon: 'none' })
      return 2
    }
    return 3
  }

  async function asyncLoginInfo() {
    const res2 = await getInfo()
    const resData = res2.data as unknown as IloginInfo
    if (resData)
      setLoginInfoRes(resData)
  }

  return {
    state,
    setStateToken,
    clearToken,
    setStateInfo,
    setStateRole,
    setStateCode,
    setStateAppId,
    login,
    setLoginRes,
    onClickDis,
    setLoginInfoRes,
    asyncLoginInfo,
  }
})
